#include <stdio.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_watchdog.h"
#include "iot_pwm.h"
#include "app_aht20.h"
#include "app_mq2.h"
#include "uart_control.h"

#define LED_INTERVAL_TIME_US 300000
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25

static int g_beepState = 0;
#define IOT_PWM_PORT_PWM0   0
#define IOT_PWM_BEEP        9
extern int beep_T;
extern int beep_H;
extern int beep_C;
extern unsigned char d_d;


static void *PWMBeepTask(const char *arg)
{
    while (1) {

            g_beepState = beep_C + beep_H + beep_T + d_d;
            if (g_beepState > 0) {
               IoTPwmStart(IOT_PWM_PORT_PWM0, 50, 4000);
            }

            else  {
                IoTPwmStop(IOT_PWM_PORT_PWM0);
            }
        usleep(10000); // 休眠10ms
        usleep(10000); // 休眠10ms
    }
}

static void StartPWMBeepTask(void)
{
    osThreadAttr_t attr;

    IoTGpioInit(IOT_PWM_BEEP);
    IoSetFunc(IOT_PWM_BEEP, 5); /* 设置IO5的功能 */
    IoTGpioSetDir(IOT_PWM_BEEP, IOT_GPIO_DIR_OUT);
    IoTPwmInit(IOT_PWM_PORT_PWM0);
    IoTWatchDogDisable();

    attr.name = "PWMBeepTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024; /* 堆栈大小为1024 */
    attr.priority = 25;

    if (osThreadNew((osThreadFunc_t)PWMBeepTask, NULL, &attr) == NULL) {
        printf("[StartPWMBeepTask] Failed to create PWMBeepTask!\n");
    }
}

APP_FEATURE_INIT(StartPWMBeepTask);
